package com.zcx.qshop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.view.rebound.ReboundListView;
import com.zcx.qshop.R;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.adapter.AddressManageAdapter;
import com.zcx.qshop.conn.JsonAddressAsyGet;
import com.zcx.qshop.conn.JsonAddressdeleteAsyGet;
import com.zcx.qshop.conn.JsonAddresssetAsyGet;
import com.zcx.qshop.dialog.WhetherDialog;
import com.zcx.qshop.view.TitleView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/8/18 0018.
 */
public class AddressManageActivity extends QSActivity {

    public static OnAddressChangeListener onAddressChangeListener;

    @BoundView(R.id.title_view) private TitleView titleView;

    @BoundView(R.id.address_manage_list_view) private ReboundListView reboundListView;

    @BoundView(R.id.address_manage_create) private View create;

    private AddressManageAdapter adapter;

    private List<JsonAddressAsyGet.Info.Address> list = new ArrayList<>();

    private JsonAddressAsyGet jsonAddressAsyGet = new JsonAddressAsyGet(QSApplication.QSPreferences.readUid(), new AsyCallBack<JsonAddressAsyGet.Info>(){

        @Override
        public void onSuccess(String toast, int type, JsonAddressAsyGet.Info info) throws Exception {

            list.addAll(info.addresses);

            adapter.notifyDataSetChanged();

            reboundListView.start();

        }

    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_address_manage);

        titleView.showStyle(TitleView.Style.FULL);

        titleView.showType(TitleView.Type.BACK_TWO, TitleView.Type.NAME_ONE);

        titleView.setTitleName("收货地址管理");

        titleView.setOnTitleItemClickListener(new TitleView.OnTitleItemClickListener() {

            @Override
            public void onBackClick() {

                finish();

            }

        });

        reboundListView.setAdapter(adapter = new AddressManageAdapter(AddressManageActivity.this, reboundListView, list) {

            @Override
            protected void onSelectChange(JsonAddressAsyGet.Info.Address address) {

                new JsonAddresssetAsyGet(QSApplication.QSPreferences.readUid(), address.aid, new AsyCallBack() {

                    @Override
                    public void onSuccess(String toast, int type, Object o) throws Exception {

                        onAddressChangeListener.onAddressChange();

                    }

                }).execute(AddressManageActivity.this);

            }

        });

        reboundListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

                if (ConfirmOrderActivity.onOrderChangeListener != null) {

                    ConfirmOrderActivity.onOrderChangeListener.onAddressChange(adapter.getItem(position));

                    finish();

                }else if (OrderDetailActivity.onOrderChangeCallBack != null) {

                    OrderDetailActivity.onOrderChangeCallBack.onAddressChange(adapter.getItem(position));

                    finish();

                }else {

                    startVerifyActivity(CreateAddressActivity.class, new Intent().putExtra("Address", adapter.getItem(position)));

                }

            }

        });

        reboundListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {

                new WhetherDialog(AddressManageActivity.this, "删除当前地址"){

                    @Override
                    protected void onYes() {

                        new JsonAddressdeleteAsyGet(QSApplication.QSPreferences.readUid(), list.get(position).aid, new AsyCallBack() {

                            @Override
                            public void onSuccess(String toast, int type, Object o) throws Exception {

                                list.remove(position);

                                adapter.notifyDataSetChanged();

                            }

                        }).execute(AddressManageActivity.this);

                    }

                }.show();

                return true;

            }

        });

        onAddressChangeListener = new OnAddressChangeListener() {

            @Override
            public void onAddressChange() {

                list.clear();

                adapter.notifyDataSetChanged();

                jsonAddressAsyGet.execute(AddressManageActivity.this);

            }

        };

        create.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                startVerifyActivity(CreateAddressActivity.class);

            }

        });

        onAddressChangeListener.onAddressChange();

    }

    @Override
    protected void onDestroy() {

        onAddressChangeListener = null;

        super.onDestroy();

    }

    public interface OnAddressChangeListener{

        void onAddressChange();

    }

}
